package server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author Rafael
 * 
 */
public class MessengerImpl extends UnicastRemoteObject implements IMessenger {

	Person person;

	protected MessengerImpl() throws RemoteException {
		super();
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void createPerson(Person person) throws RemoteException {
		//this.person = person;
		Server.persons.put(person.getName(), person);

	}

	@Override
	public Person loadPerson(String name) throws RemoteException {
		Person person = Server.persons.get(name);
		if(person != null){
			return person;
		}
		return null;
	}

	@Override
	public String deletePerson(String name) throws RemoteException {
		Person deletedPerson = Server.persons.remove(name);
		if(deletedPerson == null){
			return "Pessoa nao encontrada";
		}
		return deletedPerson.getName() + " removido(a) com sucesso";
		
	}

	@Override
	public ArrayList<Person> getContactList() throws RemoteException {
		ArrayList<Person> contacts = new ArrayList<Person>();
		if(!Server.persons.isEmpty()){
			Collection<Person> col = Server.persons.values();		
			Iterator<Person> it = col.iterator();
			while(it.hasNext()){
				contacts.add(it.next());
			}
			
		}
		return contacts;	
	}

}
